import requests
import xlrd

def kdocs_download():
    file_id = '90352137291'
    group_id = '15111116'

    kdoc_url = 'https://www.kdocs.cn/3rd/drive/api/v3/groups/%s/files/%s/download?isblocks=false'%(group_id, file_id)

    user_agent = {
        'user-agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}

    cookie = dict(
        Default='DESC-mtime',Sharing = 'DESC-file_mtime',
        wpsua='V1BTVUEvMS4wICh3ZWIta2RvY3M6Q2hyb21lXzg2LjAuNDI0MC4xOTg7IE1pY3Jvc29mdCBXaW5kb3dzOldpbmRvd3MgMTAuMDsgejF6R2w0aHZUTE9acGRIbW5kTE16UT09OmRXNXJibTkzSUNCMWJtdHViM2M9KSB1bmtub3cvdW5rbm93', \
        appcdn='qn.cache.wpscdn.cn', wps_sid='V02SkIiFBgzOssRGh1pSgCUFOaY1Z1o00a5a52800000e693cc',
        csrf='pJ6tNB5eDJNaAQh4SSXBN5jXkpzihX66', weboffice_cdn='9', lang='zh-CN', visitorid='566697402')

    response = requests.get(kdoc_url, headers=user_agent, cookies=cookie)
    print(response.content)

    # 需要加入对sha1的判断

    if response.status_code == 200:
        response_json = response.json()
        static_url = response_json['fileinfo']['url']
        response_excel = requests.get(static_url, headers=user_agent, cookies=cookie)
        excel_content = response_excel.content # 拿到excel内容的bytes数组

        excel = xlrd.open_workbook(file_contents=excel_content)
        print(excel.sheet_names())
        return excel

    else:
        print('fileinfo get failed.')

